package org.jeecg.modules.lab.items.ibasis.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.cronJob.MyEnum;
import org.jeecg.common.cronJob.entity.LabCronJob;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.lab.cronJob.service.ILabCronJobService;
import org.jeecg.modules.lab.documents.entity.LabDocuments;
import org.jeecg.modules.lab.documents.management_technology.lab_technology.controller.LabTechnologyController;
import org.jeecg.modules.lab.documents.management_technology.lab_technology.entity.LabTechnology;
import org.jeecg.modules.lab.documents.management_technology.lab_technology.service.impl.LabTechnologyServiceImpl;
import org.jeecg.modules.lab.documents.service.ILabDocumentsService;
import org.jeecg.modules.lab.items.entity.LabItemsBasis;
import org.jeecg.modules.lab.items.ibasis.service.ILabItemsBasisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description: 项目依据信息表
 * @Author: jeecg-boot
 * @Date: 2023-08-24
 * @Version: V1.0
 */
@Api(tags = "项目依据信息表")
@RestController
@RequestMapping("/ibasis/labItemsBasis")
@Slf4j
public class LabItemsBasisController extends JeecgController<LabItemsBasis, ILabItemsBasisService> {
    @Autowired
    private ILabItemsBasisService labItemsBasisService;
    @Autowired
    private ILabCronJobService labCronJobService;
    @Autowired
    private ILabDocumentsService labDocumentsService;

    /**
     * 分页列表查询
     *
     * @param labItemsBasis
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "项目依据信息表-分页列表查询")
    @ApiOperation(value = "项目依据信息表-分页列表查询", notes = "项目依据信息表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<LabItemsBasis>> queryPageList(LabItemsBasis labItemsBasis,
                                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                      HttpServletRequest req) {
        QueryWrapper<LabItemsBasis> queryWrapper = QueryGenerator.initQueryWrapper(labItemsBasis, req.getParameterMap());
        Page<LabItemsBasis> page = new Page<LabItemsBasis>(pageNo, pageSize);
        IPage<LabItemsBasis> pageList = labItemsBasisService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param labItemsBasis
     * @return
     */
    @AutoLog(value = "项目依据信息表-添加")
    @ApiOperation(value = "项目依据信息表-添加", notes = "项目依据信息表-添加")
//	@RequiresPermissions("ibasis:lab_items_basis:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody LabItemsBasis labItemsBasis) {
        labItemsBasisService.save(labItemsBasis);

        if (!"".equals(labItemsBasis.getAtta())) {
            LabDocuments labDocuments = new LabDocuments();
            labDocuments.setManagements("技术类");
            labDocuments.setDocType("标准");
            labDocuments.setDisplayName(labItemsBasis.getName());
            labDocuments.setCountdocId(labItemsBasis.getId());
            labDocuments.setUploadFiles(labItemsBasis.getAtta());
            labDocuments.setSynFlag("0");
            labDocumentsService.save(labDocuments);
        }

        if (MyEnum.sync) {
            long time = new Date().getTime();
            List<LabCronJob> jobs = new ArrayList<>();
            jobs.add(new LabCronJob("lab_items_basis", "0", time, 1));
            jobs.add(new LabCronJob("lab_documents", "0", time, 2));
            labCronJobService.saveBatch(jobs);
        }
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param labItemsBasis
     * @return
     */
    @AutoLog(value = "项目依据信息表-编辑")
    @ApiOperation(value = "项目依据信息表-编辑", notes = "项目依据信息表-编辑")
//	@RequiresPermissions("ibasis:lab_items_basis:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody LabItemsBasis labItemsBasis) {
        labItemsBasis.setSynFlag("1");
        labItemsBasisService.updateById(labItemsBasis);

        if (MyEnum.sync) {
            long time = new Date().getTime();
            LabCronJob job = new LabCronJob("lab_items_basis", "1", time, 1);
            labCronJobService.save(job);
        }
        QueryWrapper<LabDocuments> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("countdoc_id", labItemsBasis.getId());
        LabDocuments one = labDocumentsService.getOne(queryWrapper);
        if (one == null && !"".equals(labItemsBasis.getAtta())) {
            LabDocuments labDocuments = new LabDocuments();
            labDocuments.setManagements("技术类");
            labDocuments.setDocType("标准");
            labDocuments.setCountdocId(labItemsBasis.getId());
            labDocuments.setUploadFiles(labItemsBasis.getAtta());
            labDocuments.setDisplayName(labItemsBasis.getName());
            labDocuments.setSynFlag("0");
            labDocumentsService.save(labDocuments);
            if (MyEnum.sync) {
                long time = new Date().getTime();
                LabCronJob job = new LabCronJob("lab_documents", "0", time, 2);
                labCronJobService.save(job);
            }
        } else {
            one.setUploadFiles(labItemsBasis.getAtta());
            one.setSynFlag("1");
            labDocumentsService.updateById(one);
            if (MyEnum.sync) {
                long time = new Date().getTime();
                LabCronJob job = new LabCronJob("lab_documents", "1", time, 3);
                labCronJobService.save(job);
            }
        }

        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "项目依据信息表-通过id删除")
    @ApiOperation(value = "项目依据信息表-通过id删除", notes = "项目依据信息表-通过id删除")
//	@RequiresPermissions("ibasis:lab_items_basis:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        labItemsBasisService.removeById(id);

        QueryWrapper<LabDocuments> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("countdoc_id", id);
        LabDocuments one = labDocumentsService.getOne(queryWrapper);
        String conditionB = "";
        if (one != null) {
            conditionB = "id='" + one.getId() + "'";
            labDocumentsService.removeById(one);
        }

        if (MyEnum.sync) {
            long time = new Date().getTime();
            String conditionA = "id='" + id + "'";
            LabCronJob jobA = new LabCronJob("lab_items_basis", "2", conditionA, time, 1);
            labCronJobService.save(jobA);
            LabCronJob jobB = new LabCronJob("lab_documents", "2", conditionB, time, 2);
            labCronJobService.save(jobB);
        }
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "项目依据信息表-批量删除")
    @ApiOperation(value = "项目依据信息表-批量删除", notes = "项目依据信息表-批量删除")
//	@RequiresPermissions("ibasis:lab_items_basis:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        String[] bIds = ids.split(",");
        labItemsBasisService.removeByIds(Arrays.asList(bIds));
        QueryWrapper<LabDocuments> queryWrapper = new QueryWrapper<>();
        List<String> listIdA = new ArrayList<>();
        for (int i = 0; i < bIds.length; i++) {
            String id = bIds[i];
            queryWrapper.clear();
            queryWrapper.eq("countdoc_id", id);
            LabDocuments labDocuments = labDocumentsService.getOne(queryWrapper);
            if (labDocuments != null) {
                listIdA.add(labDocuments.getId());
                labDocumentsService.removeById(labDocuments);
            }
        }
        if (MyEnum.sync) {
            long time = new Date().getTime();
            labCronJobService.saveCronBatch("lab_items_basis", "2", time, bIds);
            String[] dIds = listIdA.toArray(new String[0]);
            labCronJobService.saveCronBatch("lab_documents", "2", time, dIds);
        }
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "项目依据信息表-通过id查询")
    @ApiOperation(value = "项目依据信息表-通过id查询", notes = "项目依据信息表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<LabItemsBasis> queryById(@RequestParam(name = "id", required = true) String id) {
        LabItemsBasis labItemsBasis = labItemsBasisService.getById(id);
        if (labItemsBasis == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(labItemsBasis);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param labItemsBasis
     */
//    @RequiresPermissions("ibasis:lab_items_basis:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LabItemsBasis labItemsBasis) {
        return super.exportXls(request, labItemsBasis, LabItemsBasis.class, "项目依据信息表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
//    @RequiresPermissions("ibasis:lab_items_basis:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, LabItemsBasis.class);
    }

    //获取对应的实验标准
    @GetMapping(value = "/getOne")
    public Result<LabItemsBasis> getOne(@RequestParam String name, @RequestParam(name = "itemId") String itemId) {
        LabItemsBasis labItemsBasis = labItemsBasisService.queryOne(name, itemId);
//		List<LabItemsBasis> itemsBasis = labItemsBasisService.list(new LambdaQueryWrapper<LabItemsBasis>().like(LabItemsBasis::getName, name));

        return Result.ok(labItemsBasis);
    }
}
